package com.example.test622.ui.video

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.util.Log
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.databinding.BindingAdapter
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.example.common.CommonAdapter
import com.example.common.CommonViewHolder
import com.example.net.event.bus
import com.example.common.mvi.BaseFragment
import com.example.net.ConstantValue
import com.example.net.Dao.DataBase
import com.example.net.entity.SimpleVideoEntity
import com.example.net.event.Event
import com.example.test622.BR
import com.example.test622.R
import com.example.test622.databinding.FragmentVideoBinding
import com.example.test622.databinding.SimplevideoBinding
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import com.umeng.socialize.ShareAction
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMWeb
import kotlinx.coroutines.launch


@Route(path = "/home/video")
class VideoFragment : BaseFragment<FragmentVideoBinding, VideoViewModel>() {
	var flag: Boolean = false
	 var  entity:SimpleVideoEntity?=null
	var list: MutableList<SimpleVideoEntity> = mutableListOf()
	val adapter by lazy {
		object : CommonAdapter<SimpleVideoEntity>({ R.layout.simplevideo to BR.video }) {
			@SuppressLint("UseCompatLoadingForDrawables")
			override fun onBindViewHolder(holder: CommonViewHolder, position: Int) {
				super.onBindViewHolder(holder, position)
				val videoBinding = holder.binding as SimplevideoBinding
				videoBinding.tv1.setOnClickListener {
					val web = UMWeb("https://www.baidu.com/")
					web.setTitle("This is music title")//标题
//                web.setThumb(thumb);//缩略图
					web.setDescription("my description")//描述
					ShareAction(activity)
						.withMedia(web)
						.setDisplayList(SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN)
						.open()
				}
				videoBinding.dizan.setOnClickListener {
					if (flag) {
						flag = false
						val drawable = resources.getDrawable(R.drawable.ic_thumb_up)
						drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
						videoBinding.dizan.setCompoundDrawables(drawable, null, null, null)
						Toast.makeText(requireContext(), "取消点赞", Toast.LENGTH_SHORT).show()
					} else {
						flag = true
						val drawable: Drawable = resources.getDrawable(R.drawable.ic_thumb_up_check)
						drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
						videoBinding.dizan.setCompoundDrawables(drawable, null, null, null)
						Toast.makeText(requireContext(), "点赞成功", Toast.LENGTH_SHORT).show()
					}
				}
				videoBinding.love.setOnClickListener {
					if (flag) {
						flag = false
						videoBinding.love.setText("关注")
						Toast.makeText(requireContext(), "取关", Toast.LENGTH_SHORT).show()
					} else {
						flag = true
						videoBinding.love.setText("已关注")
						Toast.makeText(requireContext(), "关注", Toast.LENGTH_SHORT).show()
					}
				}
			}
		}
	}
	@Autowired
	@JvmField
	var channelid: String? = null
	var page: Int = 1
	var size: Int = 10
	var nid: String = ""
	override fun initView() {
		binding.videoRec.adapter = adapter
//		@JvmStatic
//		fun intent(){
//			viewModel.intent.trySend(VideoIntent.FollowUser())
//		}
		binding.refresh.setOnRefreshListener {
			adapter.clear()
			viewModel.intent.trySend(VideoIntent.GetVideo(channelid!!, 1, size))
			binding.refresh.finishRefresh()
		}
		binding.refresh.setOnLoadMoreListener {
			page++
			viewModel.intent.trySend(VideoIntent.GetVideo(channelid!!, page, size))
			binding.refresh.finishLoadMore()
		}
	}
	override fun initData() {
		ARouter.getInstance().inject(this)
		Log.d("++", "initData: $channelid")
		viewModel.state.observe(this) {
			when (it) {
				is VideoState.VideoSuccess -> {
					list.addAll(it.video.data)
					adapter += list
					bus.postValue("getAdapterSize" to adapter.data.size)
				}
				
				is VideoState.FollowSuccess -> {
					Toast.makeText(requireContext(), "关注成功", Toast.LENGTH_SHORT).show()
				}
				
				is VideoState.VideoFailed -> {
					Toast.makeText(requireContext(), "${it.error}", Toast.LENGTH_SHORT).show()
				}
			}
		}
		viewModel.intent.trySend(VideoIntent.GetVideo(channelid ?: return, page, size))
	
		
		
	}
	
}